/**
 * Created by liyun on 2015/10/27.
 */
var fs = require("fs");
var debug = require('debug')('routes:applyfor');
require('date-utils');
var orders = require('../models/orders').orders;
var dispatch = require('../models/Dispatch').Dispatch;

module.exports = function (app) {

    app.get('/lottery/list', list);

    app.post('/lottery/update',update);


};

function list(req,res){
    var pagenum =1;
    var dispatchs = [];
    if(req.query.page){
        pagenum = req.query.page;
    }
    if(req.query.dispatch=='true'){
         new dispatch().list(function(result){
             dispatchs = result;
             getLottery(pagenum,dispatchs,req,res);
         });
    }else{
        getLottery(pagenum,dispatchs,req,res);
    }


};



function update(req,res){
    var body = req.body;
    new orders().edit(body,function(result){
        res.send(result);
    });
};

function getLottery(pagenum,dispatchs,req,res){
    var page = ['shop_goods_term.createtime','desc',(Number(pagenum)-1)*10,10];//Ĭ��createtime����ÿҳ6��

    new orders().list(page,function(data){
        debug(data);
        if(req.query.page){
            res.send(data);
        }else{
            res.render('shop/lottery_list',{data:data,dispatchs:dispatchs});
        }
        //
    });
}